<template>
    <div id="Mychart" :option="option" :style="{ width: width, height: height }"></div>
</template>

<script>
import * as echarts from "echarts";

export default {
    name: 'MyEcharts',
    props: {
        option: {
            type: Object,
            required: true
        },
        type: {
            type: Number,
            default: 1
        },
        id: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '600px'
        },
        height: {
            type: String,
            default: '400px'
        }
    },
    data() {
        return {
            chartInstance: null
        }
    },
    mounted() {
        this.init()
    },
    methods: {
        init() {
            console.log(this.id)
            let that = this
            this.chartInstance = echarts.init(document.getElementById('Mychart'));

            this.renderChart(this.option);


            window.addEventListener("resize", () => {
                this.chartInstance.resize();
            });

        },
        renderChart(option) {
            if (this.chartInstance) {
                this.chartInstance.setOption(option);
            }
        },
        change(data) {
            console.log(data)
            this.$emit('send', data);
        }
    },
    watch: {
        option: {
            handler(newOption) {
                if (this.chartInstance) {
                    this.chartInstance.setOption(newOption);
                }
            },
            deep: true
        }
    }
}
</script>